export default {
    getCursorNode(){
        if(document.selection){
            return document.selection.createRange().parentElement();
        }
        return window.document.getSelection().focusNode.parentNode
    },

    getCursorPosition: function (element) {
        //获取当前元素内光标位置
        var pos = 0;
        if (document.selection) {/*IE*/
            element.focus();
            var range = document.selection.createRange();
            range.moveStart("character", -element.txt.value.length);
            pos = range.text.length;
        } else if (element.selectionStart) {
            pos = element.selectionStart;
        }
        return pos;
    },
    setCursorPosition: function (element, pos) {
        //设置当前元素内光标位置
        pos = pos ? pos : 0;
        if (element.createTextRange) {
            var range = element.createTextRange();
            range.collapse(true);
            range.moveEnd('character', pos);
            range.moveStart('character', pos);
            range.select();
        } else if (element.setSelectionRange) {
            element.setSelectionRange(pos, pos);
        }
    },
    getSelectText() {
      //获取选中内容
      var userSelection, text;
      if (window.getSelection) {
          // Firefox support
          userSelection = window.getSelection();
      } else if (document.selection) {
          // IE Support
          userSelection = document.selection.createRange();
      }
      if (!(text = userSelection.text)) {
          text = userSelection.toString;
      }
      return text;
  },
  setSelectText(textDom, startPos, endPos) {
    //获取选中内容
        var startPos = parseInt(startPos),
            endPos = parseInt(endPos),
            textLength = textDom.value.length;
        if(textLength){
            if(!startPos){
                startPos = 0;
            }
            if(!endPos){
                endPos = textLength;
            }
            if(startPos > textLength){
                startPos = textLength;
            }
            if(endPos > textLength){
                endPos = textLength;
            }
            if(startPos < 0){
                startPos = textLength + startPos;
            }
            if(endPos < 0){
                endPos = textLength + endPos;
            }
            if(textDom.createTextRange){
                // IE Support
                var range = textDom.createTextRange();
                range.moveStart("character",-textLength);
                range.moveEnd("character",-textLength);
                range.moveStart("character", startPos);
                range.moveEnd("character",endPos);
                range.select();
            }else{
                // Firefox support
                textDom.setSelectionRange(startPos, endPos);
                textDom.focus();
            }
        }
    },
    insertAfterText(textDom, value) {
        //光标后插入内容
        var selectRange;
        if (document.selection) {
            // IE Support
            textDom.focus();
            selectRange = document.selection.createRange();
            selectRange.text = value;
            textDom.focus();
        }else if (textDom.selectionStart || textDom.selectionStart == '0') {
            // Firefox support
            var startPos = textDom.selectionStart;
            var endPos = textDom.selectionEnd;
            var scrollTop = textDom.scrollTop;
            textDom.value = textDom.value.substring(0, startPos) + value + textDom.value.substring(endPos, textDom.value.length);
            textDom.focus();
            textDom.selectionStart = startPos + value.length;
            textDom.selectionEnd = startPos + value.length;
            textDom.scrollTop = scrollTop;
        }
        else {
            textDom.value += value;
            textDom.focus();
        }
    }
}